package com.example.administrator.myjdlc.ui;

/**
 * Created by Administrator on 2017/3/21.
 */

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

import com.example.administrator.myjdlc.AppApplication;
import com.example.administrator.myjdlc.R;


public class ToastView extends View {
    private static final int DEFAULT_TEXT_SIZE = 28;
    private static final int DEFAULT_TIME = 1000;
    private int iconWidth;
    private int iconHeight;
    private static final int DEFAULT_WIDTH;
    private static ToastView instance;
    public String text = null;
    public int textColor = -1;
    public static float textSize;
    public Bitmap icon = null;
    private int toastHeight;
    private int toastWidth;
    public int roundCornerSize;
    private int duration;
    private int animStyleId;
    private int paddingTxt;
    public int padding;
    public int padding1;
    private WindowManager mWindowManager;
    private LayoutParams mWindowParams;
    public Paint paint = null;
    private RectF rect;
    private Handler mHandler;
    public boolean isShowing = false;
    private final Runnable dismissRunnable = new Runnable() {
        public void run() {
            ToastView.this.removeFromWindow();
        }
    };

    static {
        DEFAULT_WIDTH = UiManager.screenWidth * 4 / 5;
    }

    public boolean isShowing() {
        return this.isShowing;
    }

    public ToastView(Context context) {
        super(context);
        textSize = (float) UiManager.scaledDimen(DEFAULT_TEXT_SIZE);
        this.duration = DEFAULT_TIME;
        this.icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_toast_remind);
        this.animStyleId = 16973828;
        this.paddingTxt = UiManager.scaledDimen(10);
        this.padding = UiManager.scaledDimen(20);
        this.padding1 = UiManager.scaledDimen(30);
        this.roundCornerSize = UiManager.scaledDimen(30);
        this.mHandler = new Handler();
        this.mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        this.mWindowParams = new LayoutParams();
        this.mWindowParams.format = -2;
        this.mWindowParams.flags = 152;
        this.mWindowParams.width = -2;
        this.mWindowParams.height = -2;
        this.mWindowParams.gravity = 16;
        this.mWindowParams.type = LayoutParams.TYPE_TOAST;
        this.mWindowParams.packageName = context.getPackageName();
        this.mWindowParams.windowAnimations = this.animStyleId;
        this.paint = new Paint();
        this.paint.setFlags(1);
        this.paint.setAntiAlias(true);
        this.paint.setTextAlign(Align.LEFT);
        this.rect = new RectF();
    }

    public static ToastView getInstance() {
        if(instance == null) {
            instance = new ToastView(AppApplication.getContext());
        }

        return instance;
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.setMeasuredDimension(this.toastWidth, this.toastHeight);
    }

    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    protected void onDraw(Canvas canvas) {
        this.paint.setColor(0XFF000000);
        this.rect.bottom = (float)this.getHeight();
        this.rect.right = (float)this.getWidth();
        this.paint.setAlpha(200);
        canvas.drawRoundRect(this.rect, (float)this.roundCornerSize, (float)this.roundCornerSize, this.paint);
        if(this.text != null && !"".equals(this.text)) {
            this.paint.setTextSize(textSize);
            this.paint.setColor(this.textColor);
        }

        float textWidth = this.paint.measureText(this.text);
        FontMetrics fm = this.paint.getFontMetrics();
        float textHeight = fm.descent - fm.ascent;
        float textLeft = 0.0F;
        float textTop = 0.0F;
        int lines = 1;
        float strs;
        float i;
        float bitmapWidth;
        float bitmapHeight;
        if(textWidth > (float)DEFAULT_WIDTH) {
            lines = Math.round(textWidth / (float)DEFAULT_WIDTH);
            this.toastWidth = DEFAULT_WIDTH;
            if(this.icon != null) {
                strs = 0.0F;
                i = 0.0F;
                bitmapWidth = (float)this.icon.getWidth();
                bitmapHeight = (float)this.icon.getHeight();
                strs = (float)(this.padding + UiManager.scaledDimen(15));
                i = (this.rect.right - bitmapWidth) / 2.0F;
                textTop = strs + bitmapHeight;
                canvas.drawBitmap(this.icon, i, strs, this.paint);
            }
        } else {
            this.toastWidth = (int)textWidth + UiManager.scaledDimen(this.padding * 2);
            if(this.icon != null) {
                strs = 0.0F;
                i = 0.0F;
                bitmapWidth = (float)this.icon.getWidth();
                bitmapHeight = (float)this.icon.getHeight();
                strs = (float)(this.padding + UiManager.scaledDimen(15));
                i = (this.rect.right - bitmapWidth) / 2.0F;
                textLeft = (this.rect.right - textWidth) / 2.0F;
                textTop = strs + bitmapHeight + (float) UiManager.scaledDimen(60);
                canvas.drawBitmap(this.icon, i, strs, this.paint);
            } else {
                textLeft = (this.rect.right - textWidth) / 2.0F;
                textTop = (this.rect.bottom - textHeight) / 2.0F - fm.ascent;
            }
        }

        if(lines == 1) {
            canvas.drawText(this.text, textLeft, textTop, this.paint);
        } else {
            String[] var12 = this.autoSplit(this.text, this.paint, (float)(this.toastWidth - 3 * this.padding));

            for(int var13 = 1; var13 <= lines; ++var13) {
                textLeft = (float)this.padding;
                textTop += textHeight + (float)this.paddingTxt;
                canvas.drawText(var12[var13 - 1], textLeft, textTop, this.paint);
            }
        }

    }

    public void setText(String text) {
        this.text = text;
    }

    public void setIcon(Bitmap icon) {
        this.icon = icon;
    }

    public void setIcon(int iamgeID) {
        this.icon = BitmapFactory.decodeResource(this.getResources(), iamgeID);
    }

    public void setDuration(int dt) {
        this.duration = dt;
    }

    public void setAnimation(int animStyleId) {
        this.animStyleId = animStyleId;
        this.mWindowParams.windowAnimations = this.animStyleId;
    }

    public ToastView configAnimation(int animStyleId) {
        this.animStyleId = animStyleId;
        this.mWindowParams.windowAnimations = this.animStyleId;
        return this;
    }

    public ToastView configDuration(int dt) {
        this.duration = dt;
        return this;
    }

    public ToastView configIcon(Bitmap icon) {
        this.icon = icon;
        return this;
    }

    public void show(String text) {
        if(!this.isShowing) {
            ToastView toastView = getInstance();
            this.removeFromWindow();
            toastView.setText(text);
            toastView.addToWindow();
        }

    }

    private RectF measure() {
        this.paint.setTextSize(textSize);
        float textWidth = this.paint.measureText(this.text);
        FontMetrics fm = this.paint.getFontMetrics();
        float textHeight = fm.descent - fm.ascent;
        if(textWidth > (float)DEFAULT_WIDTH) {
            int lines = Math.round(textWidth / (float)DEFAULT_WIDTH);
            this.rect.right = (float)DEFAULT_WIDTH;
            this.rect.bottom = textHeight * (float)lines + (float)(this.padding * 2);
            this.toastWidth = DEFAULT_WIDTH;
            this.toastHeight = (int)textHeight * lines + this.padding * 2 + UiManager.scaledDimen(5);
        } else {
            this.toastWidth = (int)textWidth + this.padding * 2;
            this.toastHeight = (int)textHeight + this.padding * 2 + UiManager.scaledDimen(5);
            this.rect.right = textWidth + (float)(this.padding * 2);
            this.rect.bottom = textHeight + (float)(this.padding * 2);
            if(this.toastWidth < UiManager.screenWidth * 3 / 10) {
                this.toastWidth = UiManager.screenWidth * 3 / 10;
            }
        }

        this.icon = Bitmap.createScaledBitmap(this.icon, UiManager.scaledDimen(50), UiManager.scaledDimen(50), true);
        if(this.icon != null) {
            this.toastHeight += this.icon.getHeight() + this.padding1 + UiManager.scaledDimen(20);
            this.rect.bottom += (float)(this.icon.getHeight() + this.padding1 + UiManager.scaledDimen(20));
        }

        return this.rect;
    }

    private void addToWindow() {
        this.removeFromWindow();
        this.measure();
        this.isShowing = true;
        this.mWindowParams.width = Math.round(this.rect.right);
        this.mWindowParams.height = Math.round(this.rect.bottom);
        this.mWindowManager.addView(this, this.mWindowParams);
        this.mHandler.postDelayed(this.dismissRunnable, (long)this.duration);
    }

    private void removeFromWindow() {
        if(this.getParent() != null) {
            this.isShowing = false;
            this.mWindowManager.removeView(this);
            this.mHandler.removeCallbacks(this.dismissRunnable);
        }

    }

    public void remove() {
        if(this.getParent() != null) {
            this.isShowing = false;
            this.mWindowManager.removeView(this);
            this.mHandler.removeCallbacks(this.dismissRunnable);
        }

    }

    private String[] autoSplit(String content, Paint p, float width) {
        int length = content.length();
        float textWidth = p.measureText(content);
        int start = 0;
        int end = 1;
        int i = 0;
        int lines = (int) Math.ceil((double)(textWidth / width));

        String[] lineTexts;
        for(lineTexts = new String[lines]; start < length; ++end) {
            if(p.measureText(content, start, end) > width) {
                lineTexts[i++] = (String)content.subSequence(start, end);
                start = end;
            }

            if(end == length) {
                lineTexts[i] = (String)content.subSequence(start, end);
                break;
            }
        }

        return lineTexts;
    }
}
